﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UI.teacher
{
    public partial class TaskAuction : Form
    {
        public XueMeng.BLL.TaskManager tm = new XueMeng.BLL.TaskManager();

        public int pageSize = 10;     //每页显示行数
        public int pageCount = 0;    //页数＝总记录数/每页显示行数
        public int pageCurrent = 0;   //当前页号

        public int startIndex = 1;
        public int endIndex = 11;

        public int LoginId = 0;
        public TaskAuction()
        {
            InitializeComponent();
        }

        private void PageIndex()
        {
            if (pageCurrent == 1)
            {
                startIndex = 1;
                endIndex = startIndex + pageSize;
            }
            else
            {
                startIndex = endIndex + 1;
                endIndex = startIndex + pageSize;
            }
        }

        private void TaskAuction_Load(object sender, EventArgs e)
        {
            this.LoginId = Help.UserInfo.User.E_Id;
            if (this.LoginId == 0)
            {
                MessageBox.Show("你还没有登录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            loadForm();
        }

        public void loadForm()
        {
            XueMeng.BLL.TaskManager am = new XueMeng.BLL.TaskManager();
            int num = am.GetRecordCount("Enable=0");
            pageCount = (num / pageSize);    //计算出总页数
            if ((num % pageSize) > 0) pageCount++;
            pageCurrent = 1;    //当前页数从1开始

            PageIndex();
            load_data();
        }

        private void load_data()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.DefaultCellStyle.BackColor = Color.White;
            DataSet ds = new DataSet();
            ds = tm.GetListByPage("Enable=0", "", startIndex, endIndex);
            DataTable dt = ds.Tables[0];

            List<XueMeng.Model.Task> Task = new List<XueMeng.Model.Task>();
            Task = tm.DataTableToList(dt);

            this.bindingSource1.DataSource = Task;

            this.dataGridView1.DataSource = this.bindingSource1;
            this.bindingNavigator1.BindingSource = this.bindingSource1;

            this.dataGridView1.Columns[0].Visible = false; //任务ID
            toolStripLabel3.Text = pageCount.ToString();
            toolStripTextBox1.Text = Convert.ToString(pageCurrent);
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            xueguan.DataPropertyName = null;
            S_Name.DataPropertyName = null;
            state.DataPropertyName = null;
                
            XueMeng.BLL.EmployeeManager em = new XueMeng.BLL.EmployeeManager();
            XueMeng.Model.Employee emp;

            XueMeng.BLL.StudentManager sm = new XueMeng.BLL.StudentManager();
            XueMeng.Model.Student smp;

            XueMeng.BLL.AuctionManager am = new XueMeng.BLL.AuctionManager();

            for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
            {
                string st = "";
                emp = em.GetModel((dataGridView1.Rows[i].DataBoundItem as XueMeng.Model.Task).X_Id);
                smp = sm.GetModel((dataGridView1.Rows[i].DataBoundItem as XueMeng.Model.Task).S_Id);
                string fenzhi = (dataGridView1.Rows[i].DataBoundItem as XueMeng.Model.Task).Fraction_Sta.ToString() + "-" + (dataGridView1.Rows[i].DataBoundItem as XueMeng.Model.Task).Fraction_Sto.ToString();
                dataGridView1.Rows[i].Cells[xueguan.Index].Value = emp.E_Name;
                dataGridView1.Rows[i].Cells[S_Name.Index].Value = smp.S_Name;
                dataGridView1.Rows[i].Cells[Fraction.Index].Value = fenzhi;

                DataSet ds = am.GetList("T_Id=" + (dataGridView1.Rows[i].DataBoundItem as XueMeng.Model.Task).T_Id + "and E_Id="+this.LoginId+"");

                if (ds.Tables[0].Rows.Count == 1)
                {
                    st = "[我竞标]";
                }
                dataGridView1.Rows[i].Cells[state.Index].Value = "正在进行"+st;
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
            }
        }

        private void 竞拍此任务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string T_Id = "";
            try
            {
                T_Id = this.dataGridView1.SelectedCells[0].Value.ToString();
            }
            catch
            {
                MessageBox.Show("获取任务失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            BidTask b = new BidTask();
            b.T_Id = Convert.ToInt32(T_Id);
            b.Owner = this;
            b.Show();
        }
    }
}
